package com.secsys.action.data;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.secsys.action.BaseAction;
import com.secsys.dto.AssistDto;
import com.secsys.dto.StoCapitalizationDTO;
import com.secsys.dto.StoFinanceassetdebtDTO;
import com.secsys.dto.StoFinancecashDTO;
import com.secsys.dto.StoFinanceperstockDTO;
import com.secsys.dto.StoFinanceprofitDTO;
import com.secsys.dto.StoPriceearningDTO;
import com.secsys.dto.StoRationedsharesDTO;
import com.secsys.dto.StoStockDTO;
import com.secsys.model.TbStoStock;
import com.secsys.service.StoCapitalizationService;
import com.secsys.service.StoFinanceAssetDebtService;
import com.secsys.service.StoFinanceCashService;
import com.secsys.service.StoFinancePerStockService;
import com.secsys.service.StoFinanceProfitService;
import com.secsys.service.StoPriceearningService;
import com.secsys.service.StoRationedSharesService;
import com.secsys.service.StoStockService;
import com.secsys.service.StoStockdailyService;

public class DataAction extends BaseAction<AssistDto>{
	private AssistDto assistDto;
	private final int limit=10;
	//用于页面显示数据的
	private List<StoStockDTO> pageStockList;
	
	//股票基本信息
	private StoStockDTO stockDTO;
	//近五年每股收益对比
	private List<StoPriceearningDTO> pagePriceEarningList;
	//分红扩股
	private List<StoRationedsharesDTO> pageRationedsharesList; 
	//股本变更信息
	private List<StoCapitalizationDTO> pageCapitalizationList;
	//资产与负债
	private List<StoFinanceassetdebtDTO> pageFinanceassetdebtList; 
	//现金流量
	private List<StoFinancecashDTO> pageFinancecashList;
	//利润构成与盈利能力
	private List<StoFinanceprofitDTO> pageFinanceprofitList;
	//每股指标
	private List<StoFinanceperstockDTO> pageFinanceperstockList;
	
	
	private List<StoStockDTO> stockList;
	
	private StoStockService stockService;
	private StoStockdailyService dailyService;
	private StoFinanceProfitService financeProfitService;
	private StoCapitalizationService capitalizationService;
	private StoPriceearningService priceearningService;
	private StoRationedSharesService rationedSharesService;
	private StoFinancePerStockService financePerStockService;
	private StoFinanceAssetDebtService financeAssetDebtService;
	private StoFinanceCashService financeCashService;
	private String stockCode;
	private String key;
	private String value;
	private String filePath;
	
	
	public String dataList()throws Exception{
		stockList=new ArrayList<StoStockDTO>();
		List<TbStoStock> list=stockService.findAllList();
		for (TbStoStock tbStoStock : list) {
			stockList.add(new StoStockDTO(tbStoStock)); 
//			System.out.println(tbStoStock.getStockCode()+" - "+tbStoStock.getStockName());
		}
		System.out.println(stockList.size()); 
		assistDto=new AssistDto();
		assistDto.setCurrentPages(1);
		this.dataListPages();
		return "list";
	}
	public String dataQuery()throws Exception{
		System.out.println("dataQuery222 value="+value+" key="+key);
		this.clearMessages();
		stockList=null;
		stockList=new ArrayList<StoStockDTO>();
		if(null==value || "".equals(value)){
			List<TbStoStock> list=stockService.findAllList();
			for (TbStoStock tbStoStock : list) {
				stockList.add(new StoStockDTO(tbStoStock));
			}
			System.out.println("null==value return this.dataListPages()");
			return this.dataListPages();
		}
		if ("stockCode".equals(key)) {
			stockCode=value;
			stockDTO=new StoStockDTO();
			stockDTO=stockService.findByStockCode(stockCode);
			if (null==stockDTO || "".equals(stockDTO)) {
				this.addActionMessage("没有找到需要的结果，您可查看下面的内容，或尝试修改搜索关键字。" );
				System.out.println("stockCode==equals(key) stockDTO==null return this.dataListPages()");
				return "input";
			}
			System.out.println(stockDTO.getId()+"--"+stockDTO.getStockName());
			stockList.add(stockDTO); 
		}else if ("stockName".equals(key)) {  
			String hql="from TbStoStock where stockName like '%"+value+"%'";
			List<TbStoStock> list=stockService.findByHql(hql);
			if (list==null || list.size()==0) {
				this.addActionMessage("没有找到需要的结果，您可查看下面的内容，或尝试修改搜索关键字。" );
				return "input";
			}
			for (TbStoStock tbStoStock : list) {
				stockList.add(new StoStockDTO(tbStoStock));
			}
			return this.dataListPages();
		}else if ("stockType".equals(key)) {  
			String hql="from TbStoStock where stockType like '%"+value+"%'";
			List<TbStoStock> list=stockService.findByHql(hql);
			if (list==null || list.size()==0) {
				this.addActionMessage("没有找到需要的结果，您可查看下面的内容，或尝试修改搜索关键字。" );
				return "input";
			}
			for (TbStoStock tbStoStock : list) {
				stockList.add(new StoStockDTO(tbStoStock));
			}
			return this.dataListPages();
		}else if ("stockBoard".equals(key)) {  
			String hql="from TbStoStock where stockBoard like '%"+value+"%'";
			List<TbStoStock> list=stockService.findByHql(hql);
			if (list==null || list.size()==0) {
				this.addActionMessage("没有找到需要的结果，您可查看下面的内容，或尝试修改搜索关键字。" );
				return "input";
			}
			for (TbStoStock tbStoStock : list) {
				stockList.add(new StoStockDTO(tbStoStock));
			}
			return this.dataListPages();
		}
		assistDto=new AssistDto();
		assistDto.setCurrentPages(1);
		this.dataListPages();
		return "show";
	}
	
	public String dataUpdate()throws Exception{
		TbStoStock tbStoStock=new TbStoStock();
		System.out.println(stockDTO.getStockType());
		tbStoStock.setId(stockDTO.getId());
		tbStoStock.setStockCode(stockDTO.getStockCode());
		tbStoStock.setStockName(stockDTO.getStockName());
		tbStoStock.setStockBoard(stockDTO.getStockBoard());
		tbStoStock.setStockType(stockDTO.getStockType());
		tbStoStock.setListingDate(stockDTO.getListingDate());
		tbStoStock.setManageArea(stockDTO.getManageArea());
		tbStoStock.setMainOperation(stockDTO.getMainOperation());
		tbStoStock.setCompanyNameZh(stockDTO.getCompanyNameZh());
		tbStoStock.setCompanyNameEn(stockDTO.getCompanyNameEn());
		stockService.saveOrUpdate(tbStoStock);
		return "show";
	}
	public String dataShow()throws Exception{
		System.out.println("dataShow -- stockCode="+stockCode); 
		stockDTO=stockService.findByStockCode(stockCode);
		return "show";
	}
	public String dataEdit()throws Exception{
		System.out.println("dataEdit -111- stockCode="+stockCode); 
		stockDTO=stockService.findByStockCode(stockCode);
		return "edit";
	}
	
	public String dataDelete()throws Exception{
		return "list";
	}
	public String dataAdd()throws Exception{
		System.out.println(filePath);
		String[] s=filePath.split("，");
		for (String string : s) {
			System.out.println(string.trim());
		}
		
		return "show";
	}
	
	
	public String dataListPages()throws Exception{
		pageStockList=new ArrayList<StoStockDTO>();
		//总数
		if (0 >= assistDto.getCurrentPages()) {
			assistDto.setCurrentPages(1);
		}
		int num=stockList.size(); 
		int size=num-(assistDto.getCurrentPages()-1)*limit;
		if (size<0) {
			size=-size;
		}
		size=size>limit?limit:size;
		int offset = (assistDto.getCurrentPages() -1) * limit;
		for (int i=0;i<size;i++) {
			StoStockDTO stockDTO=stockList.get(i+offset);
			pageStockList.add(stockDTO);
		}
		/**
		 * 总共多少页
		 */
		int totalPages = (num / limit);
		if (num % limit > 0) {
			totalPages++;
		}
		assistDto.setTotalPages(totalPages);
		return "list";
	}
	
	
	
	
	@Override
	public AssistDto getModel() {
		if (assistDto==null) {
			assistDto=new AssistDto();
		}
		return assistDto;
	}
	public AssistDto getAssistDto() {
		return assistDto;
	}
	public void setAssistDto(AssistDto assistDto) {
		this.assistDto = assistDto;
	}
	public List<StoStockDTO> getStockList() {
		return stockList;
	}
	public void setStockList(List<StoStockDTO> stockList) {
		this.stockList = stockList;
	}
	public StoStockService getStockService() {
		return stockService;
	}
	public void setStockService(StoStockService stockService) {
		this.stockService = stockService;
	}
	public StoStockdailyService getDailyService() {
		return dailyService;
	}
	public void setDailyService(StoStockdailyService dailyService) {
		this.dailyService = dailyService;
	}
	public StoFinanceProfitService getFinanceProfitService() {
		return financeProfitService;
	}
	public void setFinanceProfitService(StoFinanceProfitService financeProfitService) {
		this.financeProfitService = financeProfitService;
	}
	public StoCapitalizationService getCapitalizationService() {
		return capitalizationService;
	}
	public void setCapitalizationService(
			StoCapitalizationService capitalizationService) {
		this.capitalizationService = capitalizationService;
	}
	public StoPriceearningService getPriceearningService() {
		return priceearningService;
	}
	public void setPriceearningService(StoPriceearningService priceearningService) {
		this.priceearningService = priceearningService;
	}
	public StoRationedSharesService getRationedSharesService() {
		return rationedSharesService;
	}
	public void setRationedSharesService(
			StoRationedSharesService rationedSharesService) {
		this.rationedSharesService = rationedSharesService;
	}
	public StoFinancePerStockService getFinancePerStockService() {
		return financePerStockService;
	}
	public void setFinancePerStockService(
			StoFinancePerStockService financePerStockService) {
		this.financePerStockService = financePerStockService;
	}
	public StoFinanceAssetDebtService getFinanceAssetDebtService() {
		return financeAssetDebtService;
	}
	public void setFinanceAssetDebtService(
			StoFinanceAssetDebtService financeAssetDebtService) {
		this.financeAssetDebtService = financeAssetDebtService;
	}
	public StoFinanceCashService getFinanceCashService() {
		return financeCashService;
	}
	public void setFinanceCashService(StoFinanceCashService financeCashService) {
		this.financeCashService = financeCashService;
	}
	public List<StoStockDTO> getPageStockList() {
		return pageStockList;
	}
	public void setPageStockList(List<StoStockDTO> pageStockList) {
		this.pageStockList = pageStockList;
	}
	public String getStockCode() {
		return stockCode;
	}
	public void setStockCode(String stockCode) {
		this.stockCode = stockCode;
	}
	public String getKey() {
		return key;
	}
	public void setKey(String key) {
		this.key = key;
	}
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
	public StoStockDTO getStockDTO() {
		return stockDTO;
	}
	public void setStockDTO(StoStockDTO stockDTO) {
		this.stockDTO = stockDTO;
	}
	public List<StoPriceearningDTO> getPagePriceEarningList() {
		return pagePriceEarningList;
	}
	public void setPagePriceEarningList(
			List<StoPriceearningDTO> pagePriceEarningList) {
		this.pagePriceEarningList = pagePriceEarningList;
	}
	public List<StoRationedsharesDTO> getPageRationedsharesList() {
		return pageRationedsharesList;
	}
	public void setPageRationedsharesList(
			List<StoRationedsharesDTO> pageRationedsharesList) {
		this.pageRationedsharesList = pageRationedsharesList;
	}
	public List<StoCapitalizationDTO> getPageCapitalizationList() {
		return pageCapitalizationList;
	}
	public void setPageCapitalizationList(
			List<StoCapitalizationDTO> pageCapitalizationList) {
		this.pageCapitalizationList = pageCapitalizationList;
	}
	public List<StoFinanceassetdebtDTO> getPageFinanceassetdebtList() {
		return pageFinanceassetdebtList;
	}
	public void setPageFinanceassetdebtList(
			List<StoFinanceassetdebtDTO> pageFinanceassetdebtList) {
		this.pageFinanceassetdebtList = pageFinanceassetdebtList;
	}
	public List<StoFinancecashDTO> getPageFinancecashList() {
		return pageFinancecashList;
	}
	public void setPageFinancecashList(List<StoFinancecashDTO> pageFinancecashList) {
		this.pageFinancecashList = pageFinancecashList;
	}
	public List<StoFinanceprofitDTO> getPageFinanceprofitList() {
		return pageFinanceprofitList;
	}
	public void setPageFinanceprofitList(
			List<StoFinanceprofitDTO> pageFinanceprofitList) {
		this.pageFinanceprofitList = pageFinanceprofitList;
	}
	public List<StoFinanceperstockDTO> getPageFinanceperstockList() {
		return pageFinanceperstockList;
	}
	public void setPageFinanceperstockList(
			List<StoFinanceperstockDTO> pageFinanceperstockList) {
		this.pageFinanceperstockList = pageFinanceperstockList;
	}
	public String getFilePath() {
		return filePath;
	}
	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}
	
}
